#include "config.h"
#include "NetcfgCppFunctions.h"
#include "ProfileRadioMenuButton.h"
#include "Tray.h"
#include <string>

#ifdef HAVE_LIBNOTIFY
#include <libnotify/notify.h>
#endif

#include <glibmm/ustring.h>
#include <dirent.h>
#include <gtkmm/stock.h>
#include <iostream>
#include <pthread.h>
#include <vector>

using namespace std;
Tray::Tray() {

	signal_activate().connect(sigc::mem_fun(*this, 
				&Tray::on_statusicon_activated));
	signal_popup_menu().connect(
			sigc::mem_fun(*this, &Tray::on_statusicon_popup));

	#ifdef HAVE_LIBNOTIFY
	notify_init("NetCfgTray");
	#endif

	set_visible(true);
	
	active = true;
	connected = false;

	connectionIcon = "up.png";
	disconnectIcon = "down.png";

	knownProfiles = NetcfgCpp::getProfiles();

	set_from_file(disconnectIcon);
	//set_tooltip_text(Glib::ustring(NetcfgCpp::getCurrent()));

	create_main_menu();
	mainMenu->show_all();

	
}

Tray::~Tray() {}

void Tray::on_statusicon_popup(
	guint button, guint activate_time) {
	std::vector<std::string> currentProfiles = NetcfgCpp::getProfiles();
	for(int i = 0; i < currentProfiles.size(); i++) {
		if(!knownProfile(currentProfiles[i])) {
	       		knownProfiles.push_back(currentProfiles[i]);
			}
	}
	create_main_menu();
	mainMenu->show_all();
	mainMenu->popup(button, activate_time);

}

bool Tray::knownProfile(std::string profile) {
	bool found = false;

	for(int i = 0; i < knownProfiles.size(); i++)
		if(knownProfiles[i] == profile) 
			found = true;

	return found;
}

void Tray::create_main_menu() {
	mainMenu = new Gtk::Menu();
	Gtk::MenuItem *pItem = new Gtk::ImageMenuItem(Gtk::Stock::OPEN);
	
	pItem->set_label("Profiles");
	Gtk::SeparatorMenuItem *pSep = new Gtk::SeparatorMenuItem();
        
	Gtk::ImageMenuItem *pItem1 = new Gtk::ImageMenuItem(Gtk::Stock::DISCONNECT);
	pItem1->set_label("Disconnect");
	pItem1->signal_activate().connect(sigc::mem_fun(*this, &Tray::on_disconnect_click)); 


        Gtk::MenuItem *pItem2 = new Gtk::ImageMenuItem(Gtk::Stock::ABOUT);
	pItem2->set_label("About");
	pItem2->signal_activate().connect(sigc::mem_fun(*this, &Tray::on_about_click));

	profilesSubMenu = new Gtk::Menu();
		
	create_profiles_menu();
	pItem->set_submenu(*profilesSubMenu);

        Gtk::MenuItem *pItem3 = new Gtk::ImageMenuItem(Gtk::Stock::QUIT);
	pItem3->set_label("Exit");
	pItem3->signal_activate().connect(sigc::mem_fun(*this,
		&Tray::on_exit_click));

	mainMenu->add(*pItem);
	mainMenu->add(*pItem1);
	mainMenu->add(*pSep);
	mainMenu->add(*pItem2);
	
	mainMenu->add(*pItem3);
}


void Tray::create_profiles_menu() {
	profilesSubMenu = new Gtk::Menu();
	Gtk::RadioMenuItem::Group g;
	pthread_mutex_t conLock = PTHREAD_MUTEX_INITIALIZER; 
	bool firstButton = true;
	for(int i = 0; i < knownProfiles.size(); i++) { 
		std::string profile = knownProfiles[i];

		ProfileRadioMenuButton *pItem = 
			new ProfileRadioMenuButton(g, Glib::ustring(profile),conLock);
		
		if(firstButton) {
			g = pItem->get_group();
		} else {
			pItem->set_group(g);
		}	
		
		if(firstButton) firstButton = false;
		if(NetcfgCpp::getCurrent().compare(profile) == 0) {
			pItem->set_active(true);
		}
		pItem->signal_activate().connect(sigc::mem_fun(pItem,
					&ProfileRadioMenuButton::connect));
		profilesSubMenu->add(*Gtk::manage(pItem));
	}
}
			

void Tray::on_disconnect_click() {
	NetcfgCpp::disconnect(currentProfile);
}

void Tray::on_statusicon_activated() {
	// not implemented
	}

void Tray::on_exit_click() {
	this->active = false;
	Gtk::Main::quit();
}

void Tray::on_about_click() {
	Gtk::AboutDialog a;
	a.set_name("NetcfgTray");
	a.set_copyright("Copyright 2013 Gal Bartfeld");
	a.set_comments("GUI frontend for ArchLinux network utility Netcfg");
	a.run();	
}

void Tray::setConnected() {
	if(!connected) {
		this->set_from_file(connectionIcon);
		this->set_tooltip_text
			(Glib::ustring(currentProfile));
	}
	connected = true;
}

void Tray::setDisconnected() {
	if(connected) {
		#ifdef HAVE_LIBNOTIFY
		std::cout<<"Disconnecting"<<std::endl;
		char* app = "Netcfg";
		char* msg = "Disconnected.";
		NotifyNotification * Disconnected = 
			notify_notification_new(app,msg,NULL);
			
		notify_notification_show(Disconnected, NULL);	
		#endif
		this->set_tooltip_text
			(Glib::ustring("Disconnected."));	
		this->set_from_file(disconnectIcon);
	}
	connected = false;
}

void Tray::setConnection(string profile) {
	currentProfile = profile;
}
